package com.tianqu.tms.www.car.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.MarketCarInfoDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.www.car.actionform.CarPrivilegedCarDetailActionForm;

/**
 * @author zhao-qc
 * 特权车管理 - 信息录入
 */
public class CarPrivilegedCarDetailController extends TmsController{
	private Logger logger = Logger.getLogger(CarPrivilegedCarDetailController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			Car car = Car.getInstance();
			CarPrivilegedCarDetailActionForm actionForm = new CarPrivilegedCarDetailActionForm();
			actionForm = (CarPrivilegedCarDetailActionForm)uc.getInputForm();
			TmsUserProfile profile = uc.getUserProfile();
			String lPlate = Registry.getConfig(TmsConstants.TMS_DEFLICENSEPLATE_KEY);//车牌前两位
			
			//车牌号码
			String licensePlate = actionForm.getLicensePlate();
			//易碎贴编号
			String tempLicensePlate = actionForm.getTempLicensePlate() == null ? "" : actionForm.getTempLicensePlate().toUpperCase();
			//有效结束时间
			String endDate = actionForm.getEndDate();
			//备注
			String remarks = actionForm.getRemarks();
			//更新用户
			int updateUserId = profile.getUniqueId();
			
			// 获取画面输入的特权车信息
			MarketCarInfoDto marketCarInfoDto = new MarketCarInfoDto();
			// 车牌号码 前两位=配置文件时，设为空值
			if(StringUtil.isNullOrBlank(licensePlate) || lPlate.equals(licensePlate)) marketCarInfoDto.setLicensePlate("");
			else marketCarInfoDto.setLicensePlate(licensePlate);
			marketCarInfoDto.setTempLicensePlate(tempLicensePlate);
			marketCarInfoDto.setEnterTimeS(endDate);
			marketCarInfoDto.setRemark(remarks);
			marketCarInfoDto.setUpdateUserId(updateUserId);
			//插入 库存特权车辆信息表（tms_market_car_privilege）
			car.savePrivilegeCarInfo(marketCarInfoDto);
			controller.commit();
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
		
	}
	
	public void executeUpdate(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			
			Car car = Car.getInstance();
			CarPrivilegedCarDetailActionForm actionForm = new CarPrivilegedCarDetailActionForm();
			actionForm = (CarPrivilegedCarDetailActionForm)uc.getInputForm();
			String lPlate = Registry.getConfig(TmsConstants.TMS_DEFLICENSEPLATE_KEY);//车牌前两位
			
			MarketCarInfoDto marketCarDto = (MarketCarInfoDto)uc.getAttribute(TmsConstants.VIEWHELP);

			TmsUserProfile profile = uc.getUserProfile();
			// 取得修改的特权车ID
			String privilId = marketCarDto.getPrivilId();

			//车牌号码
			String licensePlate = actionForm.getLicensePlate();
			//易碎贴编号
			String tempLicensePlate = actionForm.getTempLicensePlate() == null ? "" : actionForm.getTempLicensePlate().toUpperCase();
			//易碎贴编号
			String validStatus = actionForm.getValidStatus();
			//有效结束时间
			String endDate = actionForm.getEndDate();
			//备注
			String remarks = actionForm.getRemarks();
			//更新用户
			int updateUserId = profile.getUniqueId();
			
			// 获取画面输入的特权车信息
			MarketCarInfoDto marketCarInfoDto = new MarketCarInfoDto();
			marketCarInfoDto.setPrivilId(privilId);
			// 车牌号码 前两位=配置文件时，设为空值
			if(StringUtil.isNullOrBlank(licensePlate) || lPlate.equals(licensePlate)) marketCarInfoDto.setLicensePlate("");
			else marketCarInfoDto.setLicensePlate(licensePlate);
			marketCarInfoDto.setTempLicensePlate(tempLicensePlate);
			marketCarInfoDto.setRegisterStatusPri(validStatus);
			marketCarInfoDto.setEnterTimeS(endDate);
			marketCarInfoDto.setRemark(remarks);
			marketCarInfoDto.setUpdateUserId(updateUserId);
			//更新 库存特权车辆信息表（tms_market_car_privilege）
			car.updatePrivilegeCarInfo(marketCarInfoDto);
			controller.commit();
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
		
	}


}
